package solverdualization;

import java.util.Iterator;

import tools.Vector;
import uncertainty.UncertaintySet;


public class RestMatrix {
	
	private double[][] coef;
	private double[] coefsimple;
	
	private void matrixmultiplier(double[][] mt, Vector<Double> a, Vector<Double> b, int n){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				mt[i][j] = a.get(i)*b.get(j);
			}
		}
	}
	
	private void multiexpsimple(double[] mt, Vector<Double> a, double exp, int n){
		for(int i = 0; i < n; i++){
			mt[i] += a.get(i)*exp;
		}
	}
	
	
	private void multiexp(double[][] mt, double[][] mtx1, double exp, int n){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				mt[i][j] += exp*mtx1[i][j];
			}
		}
	}

	public RestMatrix(int n, UncertaintySet set) {
		coef = new double[n][n];
		coefsimple = new double[n];
		double[][] mt = new double[n][n];
		double numcar = set.getNumVert();
		double num = (1.0/numcar)*(1.0/numcar);
		
		Iterator<Vector<Double>> it1 = set.extremePointsInt();
		Iterator<Vector<Double>> it2;
		Vector<Double> a,b;
		
		
		while(it1.hasNext()){
			it2 = set.extremePointsInt();
			a = it1.next();
			while(it2.hasNext()){
				b = it2.next();
				matrixmultiplier(mt, a, b, n);
				multiexp(coef, mt, num, n);
				multiexpsimple(coefsimple, b, num, n);
				it2.remove();
			}
			it1.remove();
		}
	}
	
	public double[][] getMatrix(){
		return coef;
	}
	
	public double[] getVector(){
		return coefsimple;
	}

}
